﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject.Core;
using Wicresoft.SMS.Data;
using Wicresoft.SMS.Common;

namespace Wicresoft.SMS.Gateway.Host
{
    public static class GlobalFactory
    {
        private class FacotryBindingTable : StandardModule
        {
            public override void Load()
            {
                Bind<IServiceConfigProvider>()
                    .To<AppConfigProvider>();
                Bind<IWESMSParams>()
                    .To<AppConfigProvider>();
                Bind<IExchangeServerInfo>()
                    .To<AppConfigProvider>();
            }
        }

        private static IKernel kernel = new StandardKernel(new FacotryBindingTable());

        public static IServiceConfigProvider CreateServiceConfigProvider()
        {
            return (IServiceConfigProvider)kernel.Get(typeof(AppConfigProvider));
        }

        public static ISMSProvider CreateSMSProvider()
        {
            return (ISMSProvider)kernel.Get(typeof(WESMSProvider));
        }

        public static IDataProvider CreateDatabaseProvider()
        {
            return DataProviderFactory.CreateDataProvider((IConnectStringProvider)kernel.Get(typeof(AppConfigProvider)));
        }

        public static IEmailDataAccess CreateEmailDatabaseProvider()
        {
            return DataProviderFactory.CreateEmailDataProvider((IConnectStringProvider)kernel.Get(typeof(AppConfigProvider)));
        }

        public static IStateCheck CreateStateChecher()
        {
            return DataProviderFactory.CreateStateChecker((IConnectStringProvider)kernel.Get(typeof(AppConfigProvider)));
        }

        public static IExchangeServerInfo CreateESinfoProvider()
        {
            return (IExchangeServerInfo)kernel.Get(typeof(AppConfigProvider));
        }
        //进程循环的最小等待时间
        public static readonly int TaskSpanBorder = 5000;
        public static IEnumerable<Task> GetAllTasks()
        {
            List<Task> tasklist = new List<Task>();
            tasklist.Add(new SendMsgTask(CreateSMSProvider(), CreateServiceConfigProvider(), CreateDatabaseProvider()));
            tasklist.Add(new QueryMsgTask(CreateSMSProvider(), CreateServiceConfigProvider(), CreateDatabaseProvider()));
            tasklist.Add(new SendEmailTask(CreateServiceConfigProvider(), CreateEmailDatabaseProvider(),CreateESinfoProvider()));
            tasklist.Add(new SendReminderTask(CreateServiceConfigProvider(),new ReminderService(new AppConfigProvider())));
            tasklist.Add(new QueryEmailTask(CreateServiceConfigProvider(), CreateEmailDatabaseProvider(), CreateESinfoProvider()));
            tasklist.Add(new SendSpecialTask(CreateServiceConfigProvider(), CreatePushMessageProvider()));
            tasklist.Add(new SendSpecialTask2(CreateServiceConfigProvider(), CreatePushMessageProvider()));
            tasklist.Add(new StateCheckerTask(CreateServiceConfigProvider(), CreateEmailDatabaseProvider(), CreateStateChecher(), CreateESinfoProvider()));
            return tasklist;
        }

        public static IPushMessageProvider CreatePushMessageProvider()
        {
            return DataProviderFactory.CreatePushMessageProvider((IConnectStringProvider)kernel.Get(typeof(AppConfigProvider)));
        }
    }
}
